<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="Logo">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏(宽度是动态的) -->
      <el-aside :width="isCollapse? '64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 
        unique-opened 是否只保持一个子菜单的展开
        :collapse-transition="false" 关闭过渡动画,若是右侧引号中的false处于布尔类型,则必须进行数据绑定!!!
        router 是否使用 vue-router 的模式，启用该模式会在激活导航时以 index 作为 path 进行路由跳转 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
        >
          <!-- 一级菜单, 
          index属性控制菜单展开项,唯一值,且为字符串 -->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id + ''">
            <template slot="title">
              <!-- 获取iconsObj中的动态属性的用法 -->
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单
            使用vue-router 的模式, 启用该模式会在激活导航式时将 index 作为 path 进行路由跳转
            此处需要将index 改造成 实际路径的格式 -->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" 
            :index="'/'+subItem.path" @click="saveNavState('/'+subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
            </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容主体区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],      // 左侧菜单数据
      isCollapse: false, // 菜单是否折叠
      activePath: '',    // 被激活的链接地址
      iconsObj: {        // 字体图标类名对象,使用菜单项ID作为key值(问题点)
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      }
    }
  },

  mounted() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    /* 退出登录 */
    logout() {
      // window.sessionStorage.clear()
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },

    /* 折叠菜单 */
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },

    /* 获取所有菜单列表 */
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
      // console.log(res)
    },

    /* 保存激活的链接状态 */
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;  // 解决默认右边框线1px导致菜单右侧影响边界的小bug
  }
  
  .toggle-button {  /* 块元素宽度为父元素的100% */
    background-color: #4a5064;
    font-size: 10px;
    line-height: 26px;
    text-align: center;
    cursor: pointer;
    letter-spacing: 0.3em;
  }
}

.iconfont {
  margin-right: 10px;
}
</style>
